//Ext.nameSpace("App");
Ext.namespace("Pub");
Ext.namespace("Run");
Ext.namespace("Init");
Pub.gridPageSize = 25;
Pub.url=function(str){
		var appUrl='';
		var arr=str.split('\?');
		if(arr.length>1) {arr[1] = '&'+arr[1];}
		else {arr[1] = '';}		
		var _arr=arr[0].split('/');
		return appUrl+'?m='+_arr[0]+'&a='+_arr[1]+arr[1];
}
Pub.ico = function(str){
	return 'Tpl/default/Public/Image/icon-'+str+'.png'
}
Pub.src = function(str){
	str = str.replace('---','Tpl/default/Public/');
	return str;
}
Pub.post = function(){
	var p_data = arguments[0] || '';
	var p_url = arguments[1] || ''; //可选 参数
	var p_fun = arguments[2] || ''; //可选 参数
	Ext.Ajax.request({
		url:p_url,
		method:"POST",
		params:p_data,
		success:function(response,opiton){
			if(p_fun!==''){
				eval(p_fun)();
			}
		},
		failure:function(){
			alert('出错');
			//alert(response.responseText);
		}
	})
}
Pub.get = function(){
	var pData = arguments[1] || '';
	var pUrl = arguments[0] || ''; //可选 参数
	var sFun = arguments[2] || ''; //可选 参数
	var fFun = arguments[3] || ''; //可选 参数
	Ext.Ajax.request({
		url:pUrl,
		//params:pData,
		success:function(response,opiton){
			if(sFun!==''){
				eval(sFun);
			}
		},
		failure:function(){
			if(fFun!==''){
				eval(fFun);;
			}
			//alert(response.responseText);
		}
	})
}
Pub.submit = function (target){
	//target.url   = target.url || ''; //可选 参数
	//target.succ  = target.succ || ''; //可选 参数
	//target.error = target.errorRun || ''; //可选 参数
	//if(Ext.isString(target.form))var form = Ext.get(target.form);
	//else
	 var form = target.form;
	
	form.getForm().submit({
	    url:target.url,	
	    success: function(form, action) {
			/*if(target.succ!=''){
				eval(target.succ());
			}*/
		   if(action.result){
		  		Ext.MessageBox.alert('Success', action.result.message);
		   }
	    },
	    failure: function(form, action) {
			//if( action.result){
		  	//	Ext.MessageBox.alert('Success', action.result.message);
		    //}
			//if(target.error!=''){
			//	eval(target.error());
			//}
	    }
	});
	target = 0;delete target;
}
Pub.tbar=function(json){
	var json = json || '';
	if(json!='')return json;
	else return [{text:'Add Something',tooltip:'Add a new row',iconCls:'add'},
				 '-',
				 {text:'Options',tooltip:'Blah blah blah blaht',iconCls:'option'},
				 '-',
				 {text:'Remove Something',
								tooltip:'Remove the selected item',
								iconCls:'remove'
							}]
}
Pub.gridBbar=function(json){
	var pS = json.pageSize || 20;
		json.ds = json.ds||json.store;
	var msgtype = json.msgSimple || false;
	var msg = '{0} 到 {1}，共 {2}';
	if(msgtype)
		msg= '{0} 到 {1}，共 {2}';
	return new Ext.PagingToolbar({
								pageSize: pS,
								store:json.ds,
								displayInfo: true,
								displayMsg:msg,
								emptyMsg:"没有记录"
							});
}
//Ext.onReady(function(){
	//-----------------------------------------------------------------------------
Pub.grid=function (config)
{
	//var isRw = false;
	var isRw = config.rw || false;
	var parmaryId = config.prId =config.prId || 'id';
	var eUrl = (config.eUrl||'?');
	var editSucc = (config.editSucc||'');
	var addSucc = (config.addSucc||'');
	delete config.prId;
	delete config.rw;
	delete config.eUrl;
	config.editSucc=null;delete config.editSucc;
	config.addSucc=null;delete config.addSucc;
	//if(config.title || config.title=='')config.header=false;
	//Ext.applyIf(config||{},{})
	if(!isRw){
		return new Ext.grid.GridPanel(config);
	}else{
		config.listeners = (config.listeners||{
			"afteredit":function(e){
				if(e.value!=e.originalValue){
					if(e.record.data[parmaryId] && (e.record.data[parmaryId]=='*'||e.record.data[parmaryId]=='0*')){
						if(addSucc!='editSucc'){
							editSucc = addSucc;
						}
					}
					Pub.post({field_name:e.field,field_value:e.value,original_value:e.originalValue,id:(e.record.data[parmaryId]||'')},eUrl,editSucc);
				}					
			}
		});
		config.clicksToEdit = 1;
		return new Ext.grid.EditorGridPanel(config);
	}
	//config.store.load();
}
Pub.win = function(config){
	if(config.id){
		var winId = config.id
		var win = Ext.get(winId);
	}else{
		var winId = Math.random();
		var win = false;
	}
	if(!win){
	 	new Ext.Window(
			Ext.applyIf(
				config||{},
				{
					title:"新窗口",
					maximizable:true,
					id:winId,
					width:580,
					height:400//,
					//border:false//,
					//items:{}
					//buttonAlign:"center"
				}
			)
		).show();
	}
	win = 0;delete win; 
}
Pub.ComboBox = function(){
	var data = arguments[0]||[[1,'是'],[0,'否']];
	if(Ext.isString(data)){
		data = data.split(",");
		for(var k in data){
			data[k] = [k,data[k]];
		}
	}
	return new Ext.form.ComboBox({
		store:new Ext.data.SimpleStore({
			fields: ['value','text'],
			data:data
		}),
		valueField: 'value',      //传送的值
        displayField :'text',     //UI列表显示的文本
		triggerAction:'all',
		mode:'local',
		value:(arguments[1]||'')
	})
}
Pub.NumToDate = function(val){
	if(Ext.isNumber(val)){
			return val.substring(0,4)+'-'+val.substring(4,6)+'-'+val.substring(6,8)
	}else return val;
}
Pub.addTab=function(config){
	    	config['id'] = config.id || Math.random();
	    	config['items'].renderTo=config.id;
	    	//config['items'].width=get.;
	        tabs.add(
	          	Ext.applyIf(config||{},{
	          		layout:"fit",
	          		//autoWidth:true,
	          		//style:'height:100%',
		            title: '项目列表',
		            iconCls: 'tabs',
		            items:{html:'CREATE TAB FINISH'},
		            closable:true,
		            bodyStyle:"background:#5a83b9 url("+Pub.src('---Image/bg-workpalce.png')+") repeat-x",
		            bbar:['-']		            
		     	})
		     ).show();
}
Pub.download=function(cfg)
	{/*alert(uneval(cfg));*/
		cfg.post = cfg.post || (cfg.param || {});
		cfg.post['X-REQUEST-WITH-X']='download-and-export';
		var k,val;
		var str = '<form id="pub-download_temp-form" name="pub-download_temp-form" method="post" action="'+(cfg.url||'')+'" target="pub-download_temp-submit-iframe">';
		for(k in cfg.post){
			val = (cfg.post[k]+'').replace(/"/g,'&quot;');/*"转字符串对象*/
			str += '<input name="'+k+'" type="hidden" value="'+val+'" />';
		}
		str += '</form><iframe id="pub-download_temp-submit-iframe" name="pub-download_temp-submit-iframe" src="" width="0" height="0" frameborder="0"></iframe>';
		var cmp = Ext.getDom('pub-download_temp-div-cmp')||false;
		if(!cmp){
			str = '<div style="display:none" id="pub-download_temp-div-cmp">'+str+'</div>';
			Ext.select('body').insertHtml('beforeEnd',str);
		}else{
			cmp.innerHTML=str;
		}
		document['pub-download_temp-form'].submit();
	}
Run.winLoad = function (config)
{
	config = config||{};
	var url = config.url||'';delete config.url;
	Pub.win(Ext.applyIf(
	config||{},
	{
		title:config.title||"新建窗口",
		autoHeight:true,
		autoWidth:true,
		maximizable:false,
		autoLoad:{url:url,scripts:true} 
	}
	))
}
Run.LogOut = function (url){
	Run.winLoad({title:"正在注销 请稍候",url:(url||Pub.url('Public/logout'))});
}